package com.elcom.player;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.graphics.Typeface;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class Subtitle {
	ArrayList<String> listSub;
	ArrayList<String> listTime;
	ArrayList<Integer> listStart;
	ArrayList<Integer> listEnd;
	private Boolean isSubOk;
	private Boolean isEnable;
	private String filepath;
	private TextView mTextSub;
	public Subtitle(String filepath, TextView mTextSub){
		isEnable = true;
		this.filepath = filepath;
		this.mTextSub = mTextSub;
		LoadSubtitle(filepath);
	}
	public void LoadSubtitle(String path){
		filepath = path;
		// Open the file
		listSub = new ArrayList<String>();
		listTime = new ArrayList<String>();
		listStart = new ArrayList<Integer>();
		listEnd = new ArrayList<Integer>();
		
		FileInputStream fstream;
		StringBuilder ss = new StringBuilder();
		try {
			fstream = new FileInputStream(filepath);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			//Read File Line By Line
			while ((strLine = br.readLine()) != null)   {
			  // Print the content on the console
				ss.append("\n"+strLine);
				if(strLine.contains("-->")){
					listTime.add(strLine);
					ss= new StringBuilder();
				}
				if(strLine.equals("") || strLine.equals(" ")){
					listSub.add(ss.toString());
				}
			}
			//Close the input stream
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0;i < listTime.size();i++){
			String start,end;
			start = listTime.get(i).split(" --> ")[0];
			end = listTime.get(i).split(" --> ")[1];
			listStart.add(stringtoMiliTime(start));
			listEnd.add(stringtoMiliTime(end));
		}
		isSubOk = isParseSubtitle();
	}
	private int stringtoMiliTime(String ss){
		double time=0;
		String[] stime;
		stime = ss.split(":");
		if(stime.length >= 3){
			stime[2]=stime[2].replace(",",".");
			time = Integer.parseInt(stime[0])*3600+ 
					Integer.parseInt(stime[1])*60+ 
					Double.parseDouble(stime[2]);
		}
		return (int)(time*1000);
	}
	
	public boolean isParseSubtitle(){
		if((listEnd.size()==listStart.size()) && 
				(listStart.size()==listSub.size()))
			return true;
		else 
			return false;
	}
	
	public String findSubTitle(int clockVideo){
		if(!isSubOk)  return "Subtitle Error";
		if(!isEnable) return null;
		for(int i=0;i<listStart.size();i++){
			if((listStart.get(i) < clockVideo) && (listEnd.get(i) > clockVideo)){
				mTextSub.setText(listSub.get(i));
				return listSub.get(i);
			}
		}
		mTextSub.setText(null);
		return null;
	}
	public void setColor(int color){
		mTextSub.setTextColor(color);
	}
	public void setSize(float size){
		mTextSub.setTextSize(size);
	}
	public void setPosition(int position){
		if(position == 0) mTextSub.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
		else if(position == 1) mTextSub.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL);
		else if(position == 2) mTextSub.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);
	}
	public void setEnable(Boolean value){
		isEnable = value;
	}
	public void setFont(String file){
		Typeface tf = Typeface.createFromFile(file);
		mTextSub.setTypeface(tf);
	}
}
